package gui2.g1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.LookAndFeel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class ChatGUIInteractive extends JFrame {

	private JLabel caption;

	private JPanel leftPanel;
	private JLabel lblUsername;
	private JTextField fldUsername;
	private JButton btnStartChat;

	private JList chatList;
	private JScrollPane chatListScrollPane;
	
	private JTable chatTable;
	
	private JTabbedPane tabs;

	private JTextField txtChat;
	private JButton btnSendText;

	private ChatModel chatModel = new ChatModel();

	public ChatGUIInteractive() {
		setTitle("Chat");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		createWidgets();
		layoutWidgets();

		setupInteractions();

		pack();
		setLocationRelativeTo(null);
	}

	private void setupInteractions() {
		btnSendText.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				String txt = txtChat.getText();
				txtChat.setText("");
				chatModel.addMessage(new ChatMessage(txt, fldUsername
						.getText()));
			}
		});

		txtChat.addCaretListener(new CaretListener() {

			{
				caretUpdate(null);
			}

			@Override
			public void caretUpdate(CaretEvent e) {
				String txt = txtChat.getText();
				boolean enableButton = txt != null && !txt.trim().isEmpty();
				btnSendText.setEnabled(enableButton);
			}
		});

		// txtChat.getDocument().addDocumentListener(new DocumentListener() {
		//
		// @Override
		// public void removeUpdate(DocumentEvent e) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// @Override
		// public void insertUpdate(DocumentEvent e) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// @Override
		// public void changedUpdate(DocumentEvent e) {
		// // TODO Auto-generated method stub
		//
		// }
		// });

		leftPanel.addMouseMotionListener(new MouseAdapter() {
			@Override
			public void mouseMoved(MouseEvent e) {
				if (e.getX() <= 20 && e.getY() >= 50) {
					chatModel.addMessage(new ChatMessage("AFK", fldUsername
							.getText()));
				}
			}
		});
	}

	private void createWidgets() {
		caption = new JLabel("CHAT");
		caption.setPreferredSize(new Dimension(500, 50));
		caption.setHorizontalAlignment(SwingConstants.CENTER);
		caption.setFont(caption.getFont().deriveFont(25f));

		lblUsername = new JLabel("Username:");
		fldUsername = new JTextField();
		fldUsername.setMaximumSize(new Dimension(220, 25));

		btnStartChat = new JButton("Start Chat!");

		chatList = new JList();
		
		chatTable = new JTable();
		
		ChatTableModel chatTableModel = new ChatTableModel();
		chatModel.addChatListener(chatTableModel);
		
		chatTable.setModel(chatTableModel);
		
	

		ChatListener l = new ChatListModel();
		chatModel.addChatListener(l);

		chatList.setModel((ListModel) l);
		chatList.setPreferredSize(new Dimension(300, 300));
		chatList.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		chatListScrollPane = new JScrollPane(chatList);

		tabs = new JTabbedPane();
		tabs.addTab("Als Liste", chatListScrollPane);
		tabs.addTab("Als Table", chatTable);
		
		
		txtChat = new JTextField();

		btnSendText = new JButton("Send");
	}

	private void layoutWidgets() {
		getContentPane().setLayout(new BorderLayout(5, 5));

		getContentPane().add(caption, BorderLayout.NORTH);

		leftPanel = new JPanel();
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));

		leftPanel.add(lblUsername);
		leftPanel.add(fldUsername);
		leftPanel.add(btnStartChat);

		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(2, 2, 2, 2);
		centerPanel.add(tabs, c);

		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		centerPanel.add(txtChat, c);

		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		centerPanel.add(btnSendText, c);

		getContentPane().add(leftPanel, BorderLayout.WEST);
		getContentPane().add(centerPanel, BorderLayout.CENTER);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				
				try {
					//UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
					
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (UnsupportedLookAndFeelException e) {
					e.printStackTrace();
				}

				JFrame f = new ChatGUIInteractive();
				f.setVisible(true);

				JOptionPane.showMessageDialog(f, "Willkommen im Chat");

				System.out.println("Dialog geschlossen");
			}
		});
	}

}
